//
//  DiseaseTypeViewController.m
//  名医通
//
//  Created by WGP on 16/5/4.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "DiseaseTypeViewController.h"
#import "YZDiseaseTypeViewModel.h"
#import "YZDiseaseTypeModel.h"
#import "YZTableViewCell.h"
#import <MJRefresh.h>

@interface DiseaseTypeViewController ()

@property(nonatomic,strong)NSArray *models;

@end

static NSString *ReuseIdentifier = @"cell";
@implementation DiseaseTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    searchBar.placeholder = @"疾病细分搜索";
    
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = [UIColor redColor];
    self.navigationItem.titleView = searchBar;

    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchItemDidClick)];
    
//    下拉刷新控件
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.tableView.mj_header endRefreshing];
        });
        
    }];
//    上拉刷新控件
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.tableView.mj_footer endRefreshing];
        });
    }];
    
    [self.tableView registerClass:[YZTableViewCell class] forCellReuseIdentifier:ReuseIdentifier];
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [YZDiseaseTypeViewModel loadDiseaseTypeWith:self.ci1_id finished:^(NSArray *array, NSError *error) {
        if (error != nil) {
            NSLog(@"加载网络出错!");
        }
        self.models = array;
        [self.tableView reloadData];
    }];
    
}

-(void)searchItemDidClick
{
    NSLog(@"XX");
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentifier forIndexPath:indexPath];
    cell.textLabel.text = [self.models[indexPath.row] ci3_name];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YZDiseaseTypeModel *model = self.models[indexPath.row];
    
    if (self.diseaseTypeBlock) {
        self.diseaseTypeBlock(model);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
